import axios from "axios";
import {
  Toast
} from "mint-ui";
import qs from "querystring";
// 创建axios实例
const service = axios.create({
  // baseURL: 'http://127.0.0.1:8003', // api的base_url
  timeout: 35000 // 请求超时时间
});
// request拦截器
service.interceptors.request.use(
  config => {
    config.headers["accessToken"] = sessionStorage.getItem("TOKEN") || "";
    config.headers["Content-Type"] = 'application/json';
    return config;
  },
  error => {
    console.log(error); // for debug
    Promise.reject(error);
  }
);
// respone拦截器
service.interceptors.response.use(
  response => {
    /**
     * code为非200是抛错 可结合自己业务进行修改
     */
    const res = response.data;
    if (res.statusCode !== 200) {
      if (res.statusCode == 10000) {
        Toast({
          message: res.statusMessage,
          position: "bottom"
        });
      } else {
        Toast({
          message: "服务器异常!",
          position: "bottom"
        });
      }

    } else {
      return response.data;
    }
  },
  error => {
    return Promise.reject(error);
  }
);

// get请求
async function get(url) {
  const token = sessionStorage.getItem("TOKEN") || "";
  try {
    const requestConfig = {
      headers: {
        "X-Auth-Token": token
      }
    };
    const urlStr = `${url}`;
    const response = await axios.get(urlStr, requestConfig);
    if (response.data.statusCode == 10002) {
      Toast({
        message: "服务器异常!",
        position: "bottom"
      });
      return Promise.reject("Token过期，请重新登录");
    } else {
      return response.data;
    }
  } catch (error) {
    console.log("error", error);
    Toast({
      message: "服务器异常!",
      position: "bottom"
    });
  } finally {}
}
// post请求
async function post(url, param) {
  const token = sessionStorage.getItem("TOKEN") || "";
  try {
    const requestConfig = {
      headers: {
        "Content-Type": "application/json",
        responseType: "json",
        "X-Auth-Token": token
      }
    };

    const urlStr = `${url}`;
    const paramStr = qs.stringify({ ...param
    });
    const response = await axios.post(
      urlStr,
      JSON.stringify(param),
      requestConfig
    );
    return response.data;
  } catch (error) {
    console.log("error", error);
    Toast({
      message: "服务器异常!",
      position: "bottom"
    });
  } finally {}
}
// postUrlencoded请求
async function postUrlencoded(url, param) {
  const token = sessionStorage.getItem("TOKEN") || "";
  try {
    // application/x-www-form-urlencoded; charset=UTF-8  application/json
    const requestConfig = {
      headers: {
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        responseType: "json",
        "X-Auth-Token": token
      }
    };

    const urlStr = `${url}`;
    const paramStr = qs.stringify({ ...param
    });
    const response = await axios.post(urlStr, paramStr, requestConfig);
    return response.data;
  } catch (error) {
    console.log("error", error);
    Toast({
      message: "服务器异常!",
      position: "bottom"
    });
  } finally {}
}
// formData-post请求
async function formDataType(url, param) {
  const token = sessionStorage.getItem("TOKEN") || "";
  try {
    const requestConfig = {
      headers: {
        "Content-Type": "multipart/form-data",
        responseType: "json",
        token: token
      }
    };

    const urlStr = `${url}`;
    const paramStr = qs.stringify({ ...param
    });
    const response = await axios.post(urlStr, param, requestConfig);
    return response.data;
  } catch (error) {
    console.log("error", error);
    Toast({
      message: "服务器异常!",
      position: "bottom"
    });
  } finally {}
}

export {
  get,
  post,
  formDataType,
  postUrlencoded,
  service
};
